rm(list=ls(all=TRUE))
require(plyr)
require(tidyverse)
#install.packages("dplyr")
library(dplyr)
library(ggridges)
## Good cb-friendly palette
library(ggokabeito)
## Fonts aren't necessary to reproduce.
## Comment out and change the default theme as needed.
library(myriad)
library(showtext)
library(readxl)
require(sandwich)
require(lmtest)
require(foreign)
require(reshape2)
require(data.table)
require(rstanarm)
library(quantreg)
#remotes::install_github("AllanCameron/geomtextpath")
library(geomtextpath)


setwd("C:/Users/zelizer/Dropbox/priors/Replication File/")


ggtheme_personalized <- theme(panel.background = element_blank(),
                              panel.grid = element_blank(),
                              axis.ticks = element_blank(),
                              axis.title.x = element_text(color = "black" , size = 12, face="bold"),
                              axis.title.y = element_text(color = "black" , size = 12, face="bold"),
                              axis.text.y = element_text(size = 8, colour = "black"),
                              axis.text.x = element_text(size = 8, colour = "black"),
                              panel.border = element_blank(),
                              panel.spacing = unit(0.5, "cm"),
                              legend.key = element_rect(colour = "white",fill = "white"),
                              legend.text = element_text(size=10,color="black"),
                              legend.title = element_text(size = 11, face = "bold", colour = "black"),
                              strip.text.x = element_text(size = 11, face = "bold", colour = "black"),
                              strip.background = element_rect(colour = "white", fill = "white"),
                              legend.position="none",
                              plot.margin = unit(c(0.5,0.5,0.5,0.5), "cm"))

#library(showtext)
# Once it's installed, import the fonts as before:
library(extrafont)
# fonts()
# remotes::install_version("Rttf2pt1", version = "1.3.8")
# extrafont::font_import(path = "C:/Users/Zelizer/Downloads")
# extrafont::font_import()
loadfonts(device = "win")

#showtext_auto()
#import_myriad_semi()
theme_figs <- function(){
#  theme_myriad_semi(base_family = windowsFonts()[4]) +
    theme_myriad_semi(base_family = fonts()[1],
                      subtitle_family = fonts()[1],
                      caption_family = fonts()[1],) +
    theme(
      plot.background = element_rect(color = "white"),
      plot.title = element_text(size = rel(1.5)),
      plot.subtitle = element_text(size = rel(1.2)),
      axis.title.x = element_text(size = rel(1.25)),
      axis.title.y = element_text(size = rel(1.25)),
      #      axis.text.x = element_text(size = rel(1.2)),
      axis.text.y.left = element_text(size = rel(1.2)),
      axis.text.y.right = element_text(size = rel(0.9)))
      #text=element_text(family=windowsFonts()[4]))
}
# theme_set(theme_minimal())
theme_set(theme_figs())
## Colors for manual use as needed
my_oka <- palette_okabe_ito(order = c(1, 2, 3, 5, 7),
                            alpha = NULL)

###########################################################################
####      1. Import opinions from voters     ####
###########################################################################

####################  Voters    #####
# Questions:
# Next, we would like to get your feelings toward some recent
# legislation.
#
# First, we would like you to estimate how much this legislation would
# cost if rolled out in your state. Like legislators, you will only be
# given a few pieces of information about the legislation, but we would
# like you to give your best guess of its cost to the state government.
# All bills cost between $0 - $10 million.
#
# Please use the slider to indicate how much you expect the following
# legislation to cost per year (or how much the state will lose in
# revenue if the legislation were enacted).
#
# MO
# Bill 1 proposes a constitutional amendment authorizing $63 million in
# bonds for veterans' homes. The primary cost is projected principal and
# interest payments to repay the bonds within 15 years of the issuance
# date.
#
# Bill 2 waives certain business fees with the Secretary of State's
# office for veterans-owned businesses. The various fees - including
# articles of organization, creation of LLCs or nonprofits, and name
# registration - are about $50 per filing.

# MI
# Bill 1 would allow any veteran free access to any state park or state-
# operated public boating site. There are more than 100 qualifying
# recreation areas in the state. - 2.125M
#
# Bill 2 would exempt families of veterans wounded or killed in action
# from vehicle registration fees for one vehicle. Currently, there are
# approximately 3 million registered vehicles in the state.  - 1.26M

# CO
# Bill 1 would create the State Women Veterans Office and Steering
# Committee in the Office of the Governor to ensure that women veterans
# are aware of and receive the benefits to which they are entitled. - 0.1
#
# Bill 2 authorizes the Department of Military and Veterans Affairs to
# operate a one-stop veterans facility in an underserved part of the
# state. The facility would offer services including benefits, counseling,
# housing assistance, family support, identification cards and a service
# officer. - 0.1M


data_voters_MO <- read.csv(file = "MO_voters.csv",
                           stringsAsFactors = FALSE)

data_voters_MI <- read.csv(file = "MI_voters.csv",
                           stringsAsFactors = FALSE)

data_voters_CO <- read.csv(file = "CO_voters.csv",
                           stringsAsFactors = FALSE)


data_voters <- rbind.data.frame(
  data.frame(
    voter = data_voters_MO$ResponseId ,
    edu = data_voters_MO$educ_7_demos,
    gender = data_voters_MO$gender_demos,
    race = data_voters_MO$race_demos,
    ethnicity = data_voters_MO$ethnicity_demos,
    party = data_voters_MO$pid_3,
    ideo = data_voters_MO$ideo_7_1,
    attent = data_voters_MO$pol_attent,
    state = "MO" ,
    bill = "MO 1",
    estimate = data_voters_MO$bill1_low_1,
    cost = 6,
    t = "Low",
    support = data_voters_MO$bill1_low_support,
    stringsAsFactors = F
  ),
  data.frame(
    voter = data_voters_MO$ResponseId ,
    edu = data_voters_MO$educ_7_demos,
    gender = data_voters_MO$gender_demos,
    race = data_voters_MO$race_demos,
    ethnicity = data_voters_MO$ethnicity_demos,
    party = data_voters_MO$pid_3,
    ideo = data_voters_MO$ideo_7_1,
    attent = data_voters_MO$pol_attent,
    state = "MO" ,
    bill = "MO 2",
    estimate =  data_voters_MO$bill2_low_1,
    cost = 1,
    t = "Low",
    support = data_voters_MO$bill2_low_support,
    stringsAsFactors = F
  ),
  data.frame(
    voter = data_voters_MO$ResponseId ,
    edu = data_voters_MO$educ_7_demos,
    gender = data_voters_MO$gender_demos,
    race = data_voters_MO$race_demos,
    ethnicity = data_voters_MO$ethnicity_demos,
    party = data_voters_MO$pid_3,
    ideo = data_voters_MO$ideo_7_1,
    attent = data_voters_MO$pol_attent,
    state = "MO" ,
    bill = "MO 1",
    estimate = data_voters_MO$bill1_high_1,
    cost = 6,
    t = "High",
    support = data_voters_MO$bill1_high_support,
    stringsAsFactors = F
  ),
  data.frame(
    voter = data_voters_MO$ResponseId ,
    edu = data_voters_MO$educ_7_demos,
    gender = data_voters_MO$gender_demos,
    race = data_voters_MO$race_demos,
    ethnicity = data_voters_MO$ethnicity_demos,
    party = data_voters_MO$pid_3,
    ideo = data_voters_MO$ideo_7_1,
    attent = data_voters_MO$pol_attent,
    state = "MO" ,
    bill = "MO 2",
    estimate =  data_voters_MO$bill2_high_1,
    cost = 1,
    t = "High",
    support = data_voters_MO$bill2_high_support,
    stringsAsFactors = F
  )
)

data_voters <- rbind.data.frame(data_voters ,
                                rbind.data.frame(
                                  data.frame(
                                    voter = data_voters_MI$ResponseId ,
                                    edu = data_voters_MI$educ_7_demos,
                                    gender = data_voters_MI$gender_demos,
                                    race = data_voters_MI$race_demos,
                                    ethnicity = data_voters_MI$ethnicity_demos,
                                    party = data_voters_MI$pid_3,
                                    ideo = data_voters_MI$ideo_7_1,
                                    attent = data_voters_MI$pol_attent,
                                    state = "MI" ,
                                    bill = "MI 1",
                                    estimate = data_voters_MI$bill1_low_1,
                                    cost = 2.125,
                                    t = "Low",
                                    support = data_voters_MI$bill1_low_support,
                                    stringsAsFactors = F
                                  ),
                                  data.frame(
                                    voter = data_voters_MI$ResponseId ,
                                    edu = data_voters_MI$educ_7_demos,
                                    gender = data_voters_MI$gender_demos,
                                    race = data_voters_MI$race_demos,
                                    ethnicity = data_voters_MI$ethnicity_demos,
                                    party = data_voters_MI$pid_3,
                                    ideo = data_voters_MI$ideo_7_1,attent = data_voters_MI$pol_attent,
                                    state = "MI" ,
                                    bill = "MI 2",
                                    estimate = data_voters_MI$bill2_low_1,
                                    cost = 1.26,
                                    t = "Low",
                                    support = data_voters_MI$bill2_low_support,
                                    stringsAsFactors = F
                                  ),
                                  data.frame(
                                    voter = data_voters_MI$ResponseId ,
                                    edu = data_voters_MI$educ_7_demos,
                                    gender = data_voters_MI$gender_demos,
                                    race = data_voters_MI$race_demos,
                                    ethnicity = data_voters_MI$ethnicity_demos,
                                    party = data_voters_MI$pid_3,
                                    ideo = data_voters_MI$ideo_7_1,attent = data_voters_MI$pol_attent,
                                    state = "MI" ,
                                    bill = "MI 1",
                                    estimate = data_voters_MI$bill1_high_1,
                                    cost = 2.125,
                                    t = "High",
                                    support = data_voters_MI$bill1_high_support,
                                    stringsAsFactors = F
                                  ),
                                  data.frame(
                                    voter = data_voters_MI$ResponseId ,
                                    edu = data_voters_MI$educ_7_demos,
                                    gender = data_voters_MI$gender_demos,
                                    race = data_voters_MI$race_demos,
                                    ethnicity = data_voters_MI$ethnicity_demos,
                                    party = data_voters_MI$pid_3,
                                    ideo = data_voters_MI$ideo_7_1, attent = data_voters_MI$pol_attent,
                                    state = "MI" ,
                                    bill = "MI 2",
                                    estimate = data_voters_MI$bill2_high_1,
                                    cost = 1.26,
                                    t = "High",
                                    support = data_voters_MI$bill2_high_support,
                                    stringsAsFactors = F
                                  )
                                ))

data_voters <- rbind.data.frame(data_voters ,
                                rbind.data.frame(
                                  data.frame(
                                    voter = data_voters_CO$ResponseId ,
                                    edu = data_voters_CO$educ_7_demos,
                                    gender = data_voters_CO$gender_demos,
                                    race = data_voters_CO$race_demos,
                                    ethnicity = data_voters_CO$ethnicity_demos,
                                    party = data_voters_CO$pid_3,
                                    ideo = data_voters_CO$ideo_7_1,
                                    attent = data_voters_CO$pol_attent,
                                    state = "CO" ,
                                    bill = "CO 1",
                                    estimate = data_voters_CO$bill1_low_1,
                                    cost = 0.1,
                                    t = "Low",
                                    support = data_voters_CO$bill1_low_support,
                                    stringsAsFactors = F
                                  ),
                                  data.frame(
                                    voter = data_voters_CO$ResponseId ,
                                    edu = data_voters_CO$educ_7_demos,
                                    gender = data_voters_CO$gender_demos,
                                    race = data_voters_CO$race_demos,
                                    ethnicity = data_voters_CO$ethnicity_demos,
                                    party = data_voters_CO$pid_3,
                                    ideo = data_voters_CO$ideo_7_1,
                                    attent = data_voters_CO$pol_attent,
                                    state = "CO" ,
                                    bill = "CO 2",
                                    estimate = data_voters_CO$bill2_low_1,
                                    cost = 0.1,
                                    t = "Low",
                                    support = data_voters_CO$bill2_low_support,
                                    stringsAsFactors = F
                                  ),
                                  data.frame(
                                    voter = data_voters_CO$ResponseId ,
                                    edu = data_voters_CO$educ_7_demos,
                                    gender = data_voters_CO$gender_demos,
                                    race = data_voters_CO$race_demos,
                                    ethnicity = data_voters_CO$ethnicity_demos,
                                    party = data_voters_CO$pid_3,
                                    ideo = data_voters_CO$ideo_7_1,
                                    attent = data_voters_CO$pol_attent,
                                    state = "CO" ,
                                    bill = "CO 1",
                                    estimate = data_voters_CO$bill1_high_1,
                                    cost = 0.1,
                                    t = "High",
                                    support = data_voters_CO$bill1_high_support,
                                    stringsAsFactors = F
                                  ),
                                  data.frame(
                                    voter = data_voters_CO$ResponseId ,
                                    edu = data_voters_CO$educ_7_demos,
                                    gender = data_voters_CO$gender_demos,
                                    race = data_voters_CO$race_demos,
                                    ethnicity = data_voters_CO$ethnicity_demos,
                                    party = data_voters_CO$pid_3,
                                    ideo = data_voters_CO$ideo_7_1,
                                    attent = data_voters_CO$pol_attent,
                                    state = "CO" ,
                                    bill = "CO 2",
                                    estimate = data_voters_CO$bill2_high_1,
                                    cost = 0.1,
                                    t = "High",
                                    support = data_voters_CO$bill2_high_support,
                                    stringsAsFactors = F
                                  )
                                ))

data_voters <- subset(data_voters , !is.na(estimate))
data_voters$error <- data_voters$estimate - data_voters$cost
dim(data_voters)
head(data_voters)

table(table(data_voters$voter))

data_voters <- mutate(data_voters , 
                      party = factor(party , levels = c("Independent","Democrat",
                                                        "Republican","No Preference",
                                                        "Other"),
                                     labels = c("1 Independent","2 Democrat",
                                                "3 Republican","4 Other",
                                                "4 Other")),
                      gender = factor(gender , levels = c("Male","Female",
                                                        "Other / Prefer not to say"),
                                     labels = c("1 Male","2 Female",
                                                "3 Other / Prefer not to say")),
                      edu = factor(edu , levels = c("Less than high school",
                                                    "High school graduate",
                                                    "Some college","2 year degree",
                                                    "4 year degree","Professional degree",
                                                    "Doctorate"),
                                   labels = c("1 Less than HS","2 HS",
                                              "3 Some College","3 Some College",
                                              "4 4yr College","5 Masters +","5 Masters +")),
                      race = factor(race , levels = unique(data_voters$race) , 
                                        labels = c("2 White","3 Black",rep("1 Other/Multiple/Missing",24)),ordered = FALSE),
                      ethnicity = factor(ethnicity , levels = c("None of these","Yes",""),
                                         labels = c("1 No" , "2 Yes" , "1 No")),
                      attent = factor(attent , levels = c("Never","Some of the time",
                                                          "About half the time",
                                                          "Most of the time","Always")
                                      )
                      )

with(data_voters , table(race, ethnicity))

data_voters$race <- paste0(data_voters$race)
data_voters$race[data_voters$race=="2 White" & data_voters$ethnicity=="2 Yes"] <- "4 Hispanic"
data_voters$race <- factor(data_voters$race , levels = c("2 White","3 Black","1 Other/Multiple/Missing","4 Hispanic"))
table(data_voters$race)

data_voters_unique <- unique(subset(data_voters , select = c("voter","edu","gender","race","ethnicity",
                                       "party","ideo","attent","state")))
  
dim(data_voters_unique)
apply(data_voters_unique[,2:9] , 2 , function(x) round(prop.table(table(x))*100,1))

data_voters_unique$ideo1 <- 0
data_voters_unique$ideo1[data_voters_unique$ideo<3] <- 1
data_voters_unique$ideo1[data_voters_unique$ideo>=3 & data_voters_unique$ideo<=5] <- 2
data_voters_unique$ideo1[data_voters_unique$ideo>5] <- 3

table(data_voters_unique$state)
  
subset(data_voters_unique , state=="MO")[,2:10] %>% 
  apply(. , 2 , function(x) round(prop.table(table(x))*100,1))

subset(data_voters_unique , state=="MI")[,2:10] %>% 
  apply(. , 2 , function(x) round(prop.table(table(x))*100,1))

subset(data_voters_unique , state=="CO")[,2:10] %>% 
  apply(. , 2 , function(x) round(prop.table(table(x))*100,1))


###########################################################################
######################  0. Prior beliefs  #################################
###########################################################################



temp_data <- data_voters %>%
  subset(. , t %in% c("Low")) 
temp_data %>% with(. , quantile(error , c(0.25 , 0.5 , 0.75)))

data_voters %>%
  subset(. , t %in% c("Low") & state=="MO") %>% 
  with(. , quantile(error , c(0.25 , 0.5 , 0.75)))


with(temp_data , mean(error))
with(temp_data , mean(error>0))
with(temp_data , mean(abs(error)>5))


my_oka1 <- rep(my_oka , 2)
  #showtext_auto()
loadfonts(device="postscript")
pdf(
  "voter_beliefs1.pdf",
  height = 4,
  width = 8,
  onefile = FALSE,
  family = 'Myriad Pro'
)
temp_data %>%
  ggplot() +
  geom_histogram(data = temp_data ,
                 mapping = aes(x = error, y = ..density..*2),
                 stat = "bin",
                 bins = 20, size = 0.5,
                 alpha = 0.7) +
  geom_vline(xintercept = 0 , linetype = 1 , color = "gray30") +
  geom_step(mapping = aes(x = error, y = ..density..*2),
            bins = 20, alpha = 0.9,
            color = "gray30", size = 0.6,
            stat = "bin",
            direction = "mid") +
  geom_textvline(xintercept = 0, label = "25th Percentile = 0.0", hjust = 0.8,
                 linetype = 2, vjust = -0.3, color = my_oka1[1], fontface = "bold") +
  geom_textvline(xintercept = 2.9, label = "50th Percentile =  2.9", hjust = 0.8,
                 linetype = 2, vjust = 1.3, color = my_oka1[3], fontface = "bold") +
  geom_textvline(xintercept = 4.9, label = "75th Percentile =  4.9", hjust = 0.8,
                 linetype = 2, vjust = 1.3, color = my_oka1[5], fontface = "bold") +
  # geom_vline(data = tempdata ,
  #            aes(xintercept = cost ), color = "red" , linetype="dashed") +
  #  scale_fill_manual(values = alpha(my_oka1, 0.8)) +
  #  scale_color_manual(values = alpha(my_oka1, 1)) +
  lims(x = c(-22 , 22))  +
  guides(color = "none", fill = "none") +
  labs(x = "Error of Forecast (in $M)", y = "Density")
dev.off()


summary(m <- lm(error ~ as.numeric(party=="3 Republican") + 
                  gender + as.numeric(edu%in%c("4 4yr College","5 Masters +")) +
                  as.numeric(attent%in%c("Most of the time","Always")) + 
                  race + 
                  as.numeric(party%in%c("1 Independent","4 Other")), data = temp_data))
coeftest(m, vcov = vcovHC(m, type="HC1"))
dim(temp_data)



with(temp_data , mean(estimate - cost))
with(temp_data , mean(pred_estimate - cost))
0.9799778 / 1.389101

summary(m <- lm(error ~ as.numeric(party=="3 Republican") + 
                  gender + as.numeric(edu%in%c("4 4yr College","5 Masters +")) +
                  as.numeric(attent%in%c("Most of the time","Always")) + 
                  race + 
                  as.numeric(party%in%c("1 Independent","4 Other")), data = subset(temp_data , state=="MO")))
coeftest(m, vcov = vcovHC(m, type="HC1"))

###########################################################################
######################  1. Posterior beliefs  #############################
###########################################################################
temp_data <- data_voters %>%
  subset(. , t %in% c("Low","High"))


t.test(temp_data$error)

subset(temp_data , t=="High") %>% with(. , mean(error > 0))
subset(temp_data , t=="High") %>% with(. , quantile(error , c(0.25 , 0.5 , 0.75)))



pdf(
    "voter_beliefs_posterior1.pdf",
    height = 4,
    width = 8,
    onefile = FALSE,
    family = 'Myriad Pro'
  )
temp_data %>%
  subset(. , t=="High") %>%
  ggplot() +
  geom_histogram(mapping = aes(x = error, y = ..density..*2),
                 stat = "bin",
                 bins = 20, size = 0.5,
                 alpha = 0.7) +
  geom_vline(xintercept = 0 , linetype = 1 , color = "gray30") +
  geom_step(mapping = aes(x = error, y = ..density..*2),
            bins = 20, alpha = 0.9,
            color = "gray30", size = 0.6,
            stat = "bin",
            direction = "mid") +
  geom_textvline(xintercept = 0, label = "25th Percentile = 0.0", hjust = 0.8,
                 linetype = 2, vjust = -0.3, color = my_oka1[1], fontface = "bold") +
  geom_textvline(xintercept = 2.0, label = "50th Percentile =  2.0", hjust = 0.8,
                 linetype = 2, vjust = 1.3, color = my_oka1[3], fontface = "bold") +
  geom_textvline(xintercept = 4.9, label = "75th Percentile =  4.9", hjust = 0.8,
                 linetype = 2, vjust = 1.3, color = my_oka1[5], fontface = "bold") +
  # geom_vline(data = tempdata ,
  #            aes(xintercept = cost ), color = "red" , linetype="dashed") +
  #  scale_fill_manual(values = alpha(my_oka1, 0.8)) +
  #  scale_color_manual(values = alpha(my_oka1, 1)) +
  lims(x = c(-22 , 22))  +
  guides(color = "none", fill = "none") +
  labs(x = "Error of Forecast (in $M)", y = "Density")
dev.off()

temp_data_MO <- subset(temp_data , state=="MO")


subset(temp_data , t=="Low") %>% with(. , mean(error))
m1 <- lm(error ~ as.factor(t=="High"), data = temp_data)
coeftest(m1, vcov = vcovHC(m1, type="HC1"))
m1 <- lm(error ~ as.factor(t=="High")+ as.factor(bill) + edu + gender + party + 
           ideo + attent, data = temp_data)
coeftest(m1, vcov = vcovHC(m1, type="HC1"))

subset(temp_data_MO , t=="Low") %>% with(. , mean(error))
m1 <- lm(error ~ as.factor(t=="High")+ as.factor(bill) + edu + gender + party + 
           ideo + attent, data = temp_data_MO)
coeftest(m1, vcov = vcovHC(m1, type="HC1"))


rq(error ~ as.factor(t=="High"), data = temp_data, tau = c(0.5) , method = "fn") %>%
  summary(. , se = "boot" , bsmethod = "mcmb")
rq(error ~ as.factor(t=="High"), data = temp_data_MO, tau = c(0.5) , method = "fn") %>%
  summary(. , se = "boot" , bsmethod = "mcmb")


subset(temp_data , t=="Low") %>% with(. , mean(abs(error)))
m1 <- lm(abs(error) ~ as.factor(t=="High"), data = temp_data)
coeftest(m1, vcov = vcovHC(m1, type="HC1"))
m1 <- lm(abs(error) ~ as.factor(t=="High")+ as.factor(bill) + edu + gender + party + 
           ideo + attent, data = temp_data)
coeftest(m1, vcov = vcovHC(m1, type="HC1"))

subset(temp_data_MO , t=="Low") %>% with(. , mean(abs(error)))
m1 <- lm(abs(error) ~ as.factor(t=="High")+ as.factor(bill) + edu + gender + party + 
           ideo + attent, data = temp_data_MO)
coeftest(m1, vcov = vcovHC(m1, type="HC1"))



subset(temp_data , t=="Low") %>% with(. , mean(as.numeric(error>0)))
m1 <- lm(as.numeric(error>0) ~ as.factor(t=="High"), data = temp_data)
coeftest(m1, vcov = vcovHC(m1, type="HC1"))
m1 <- lm(as.numeric(error>0) ~ as.factor(t=="High")+ as.factor(bill) + edu + gender + party + 
           ideo + attent, data = temp_data)
coeftest(m1, vcov = vcovHC(m1, type="HC1"))

subset(temp_data_MO , t=="Low") %>% with(. , mean(as.numeric(error>0)))
m1 <- lm(as.numeric(error>0) ~ as.factor(t=="High")+ as.factor(bill) + edu + gender + party + 
           ideo + attent, data = temp_data_MO)
coeftest(m1, vcov = vcovHC(m1, type="HC1"))

summary(m1)




m <- mutate(temp_data , t = as.numeric(t=="High")) %>% 
  lm(error ~  as.numeric(edu%in%c("4 4yr College","5 Masters +"))*t + 
       gender*t + as.numeric(party=="3 Republican")*t + 
       as.numeric(party%in%c("1 Independent","4 Other"))*t+ 
       race*t + 
       #scale(ideo) + 
       as.numeric(attent%in%c("Most of the time","Always"))*t, data = .)
coeftest(m, vcov = vcovHC(m, type="HC1"))

m <- mutate(temp_data_MO , t = as.numeric(t=="High")) %>% 
  lm(error ~  as.numeric(edu%in%c("4 4yr College","5 Masters +"))*t + 
       gender*t + as.numeric(party=="3 Republican")*t + 
       as.numeric(party%in%c("1 Independent","4 Other"))*t+ 
       race*t + 
       #scale(ideo) + 
       as.numeric(attent%in%c("Most of the time","Always"))*t, data = .)
coeftest(m, vcov = vcovHC(m, type="HC1"))
str(m)

###########################################################################
####  5. Support    ####
################################
summary(lm(as.numeric(support=="Support") ~
             as.numeric(t=="High") , data = data_voters))

m <- lm(as.numeric(support=="Support")*100 ~ cost * as.numeric(t=="High")
           , data = data_voters)
coeftest(m, vcov = vcovHC(m, type="HC1"))

m <- lm(as.numeric(support=="Support")*100 ~ cost * as.numeric(t=="High") + 
             estimate * as.numeric(t=="High") , data = data_voters)
coeftest(m, vcov = vcovHC(m, type="HC1"))




m <- lm(as.numeric(support=="Support")*100 ~ cost * as.numeric(t=="High")
        , data = subset(data_voters , state=="MO"))
coeftest(m, vcov = vcovHC(m, type="HC1"))
m <- lm(as.numeric(support=="Support")*100 ~ cost * as.numeric(t=="High") + 
          estimate * as.numeric(t=="High") , data = subset(data_voters , state=="MO"))
coeftest(m, vcov = vcovHC(m, type="HC1"))
summary(m)

